<?php

namespace App\Services;

use App\Models\BookOrder;
use App\Models\BookRule;
use App\Models\BookRuleFood;
use App\Models\Food;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * Class BookRuleService
 * @package App\Services
 * 订餐规则
 */
class BookRuleService
{
    /**
     * @param Request $request
     * @return string
     * @throws \Exception
     */
    public static function add(Request $request)
    {
        DB::beginTransaction();
        try {
            $bookRule = BookRule::create(array_merge($request->all(), [
                'school_id' => \Auth::user()->school_id
            ]));
            foreach ($request->food_ids as $val) {
                if (gettype($val) === 'integer') {
                    BookRuleFood::create([
                        'rule_id' => $bookRule->id,
                        'food_id' => $val
                    ]);
                } else {
                    // 可自定义新增菜品
                    $food = new Food();
                    $food->name = $val;
                    $food->supply_type = [$bookRule->eat_type];
                    $food->school_id = \Auth::user()->school_id;
                    $food->status = Food::START_USING;
                    $food->food_type_id = 0;
                    $food->save();
                    BookRuleFood::create([
                        'rule_id' => $bookRule->id,
                        'food_id' => $food->id
                    ]);
                }
            }
            DB::commit();
            return $bookRule;
        } catch (\Exception $exception) {
            DB::rollBack();
            return false;
        }
    }

    /**
     * @param BookRule $bookRule
     * @return BookRule|bool
     * @throws \Exception
     */
    public static function edit(BookRule $bookRule)
    {
        $request = request();
        DB::beginTransaction();
        try {
            BookRuleFood::where('rule_id', $bookRule->id)->delete();
            foreach ($request->food_ids as $val) {
                if (gettype($val) === 'integer') {
                    BookRuleFood::create([
                        'rule_id' => $bookRule->id,
                        'food_id' => $val
                    ]);
                } else {
                    // 可自定义新增菜品
                    $food = new Food();
                    $food->name = $val;
                    $food->supply_type = [$bookRule->eat_type];
                    $food->school_id = \Auth::user()->school_id;
                    $food->status = Food::START_USING;
                    $food->food_type_id = 0;
                    $food->save();
                    BookRuleFood::create([
                        'rule_id' => $bookRule->id,
                        'food_id' => $food->id
                    ]);
                }
            }
            DB::commit();
            return $bookRule;
        } catch (\Exception $exception) {
            DB::rollBack();
            return false;
        }
    }

    /**
     * @param BookRule $bookRule
     * @return BookRule|bool
     * @throws \Exception
     */
    public static function cancel(BookRule $bookRule)
    {
        DB::beginTransaction();
        try {
            // 取消当前订餐规则
            $bookRule->is_cancel = BookRule::CANCELED;
            $bookRule->save();
            // 取消当前订餐规则下的所有状态为 未取消 && 未结算 的订单
            $orders = BookOrder::where('school_id', \Auth::user()->school_id)
                ->where('book_rule_id', $bookRule->id)
                ->where('is_calculated', BookOrder::NOT_CALCULATED)
                ->isNotCanceled()->get();
            foreach ($orders as $order) {
                $order->backendCancel();
            }
            DB::commit();
            return $bookRule;
        } catch (\Exception $exception) {
            DB::rollBack();
            return false;
        }
    }
}
